﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MigratorCore.Actions;
using MigratorCore.Status;

namespace MigratorWizard
{
    public partial class LogViewer : Form
    {
        private ISourceAction source;
        private IDestinationAction destination;

        public LogViewer(Main main, ISourceAction source, IDestinationAction destination)
        {
            InitializeComponent();
            this.SetDesktopLocation(main.DesktopLocation.X + 622, main.DesktopLocation.Y + 10);
            this.source = source; this.destination = destination;
            this.source.Status.Change += new MigratorCore.Bases.MirgratorChangedEventHandler(Status_Change);
            this.destination.Status.Change +=  new MigratorCore.Bases.MirgratorChangedEventHandler(Status_Change);
            listView1.Items.Clear();
            Status_Change();
        }

        delegate void SetCallback();
        int n = 0;
        void Status_Change()
        {
            try
            {
                if (this.listView1.InvokeRequired)
                {
                    SetCallback d = new SetCallback(Status_Change);
                    this.Invoke(d);
                }
                else
                {
                    List<Event> events = new List<Event>();
                    foreach (Event e in source.Status)
                        events.Add(e);
                    foreach (Event e in destination.Status)
                        events.Add(e);
                    events.Sort();

                    n++;
                    for (int x = listView1.Items.Count; x < events.Count; x++)
                        listView1.Items.Add(new ListViewItem(new string[] { events[x].Name, events[x].Date.ToShortDateString() + " " + events[x].Date.ToShortTimeString(), events[x].EventType.ToString(), events[x].Message }));
                    if (n < 2)
                        listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    else if (n > 15) n = 0;
                }
            }
            catch { }
        }

        private void LogViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.source.Status.Change -= new MigratorCore.Bases.MirgratorChangedEventHandler(Status_Change);
            this.destination.Status.Change -= new MigratorCore.Bases.MirgratorChangedEventHandler(Status_Change);
        }


    }
}
